require("./css/index.scss");
require("../common/common.scss");

import {
    createBtnHandler
} from './create';
import {
    editBtnHandler
} from './edit';

import {
    removeBtnHandler
} from './remove';

import {
    grid
} from '../common/grid';
import {
    editPwd,
    loginOut
} from '../common/settings';
import {
    renderSearchbar
} from './search';
import {
    getSectionInfo,renderSectionView
} from './renderSectionView';

$(function () {
    grid();
    window.editPwd = editPwd;
    window.loginOut = loginOut;
    let $table = $('#table').bootstrapTable({
        toolbar: '#toolbar',
        url: GLOBAL_ListUrl,

        sidePagination: "server",
        queryParamsType: '',
        queryParams: function (params) {
            let query = {};
            $.each($('.search-box form').serializeArray(), (key, val) => {
                query[val.name] = $.trim(val.value);
            });
            if(GLOBAL_cateid){
                query.secid = GLOBAL_cateid;
            }
            query = $.extend({}, params, query);
            return query;
        },
        columns: [{
            checkbox: true
        }, {
            field: 'name',
            title: '名字',
            sortable: true
        }, {
            field: 'email',
            title: '邮箱',
            sortable: true
        }, {
            field: 'section_name',
            title: '所属部门',
            sortable: true
        }, {
            field: 'work_date',
            title: '上次邮件发送时间',
            sortable: true
        }, {
            field: 'update_time',
            title: '更新时间',
            sortable: true
        }]
    });
    //多选不能编辑,删除
    $table.on('check.bs.table', () => {
        let selectedList = $('#table').bootstrapTable('getSelections');
        if (selectedList.length > 1) {
            $('#editBtn').attr('disabled', 'disabled');
            return;
        }
        $('#editBtn').removeAttr('disabled');
    });
    $table.on('uncheck.bs.table', () => {
        let selectedList = $('#table').bootstrapTable('getSelections');
        if (selectedList.length > 1) {
            $('#editBtn').attr('disabled', 'disabled');
            $('#removeBtn').attr('disabled', 'disabled');
            return;
        }
        $('#editBtn').removeAttr('disabled');
        $('#removeBtn').removeAttr('disabled');
    });
    $table.on('check-all.bs.table', (rows) => {
        $('#editBtn').attr('disabled', 'disabled');
        $('#removeBtn').attr('disabled', 'disabled');
    });
    $table.on('uncheck-all.bs.table', (rows) => {
        $('#editBtn').removeAttr('disabled');
        $('#removeBtn').removeAttr('disabled');
    });

    window.createBtnHandler = createBtnHandler;
    window.editBtnHandler = editBtnHandler;
    window.removeBtnHandler = removeBtnHandler;
    renderSearchbar();
    getSectionInfo(function (data) {
        renderSectionView(data,'#mysec');
    });
}());